package com.mojing.controller;

import lombok.AllArgsConstructor;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.zhipuai.ZhiPuAiChatOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("/zhipu")
@AllArgsConstructor
public class ZhipuChatController {

	private final ChatModel chatModel;

	@GetMapping("/hello")
	public String hello() {
		return "hello";
	}

	@GetMapping("/simple")
	public String simple(@RequestParam(name = "query") String query) {
		// 调用模型
		return chatModel.call(query);
	}

	@GetMapping("/stream/chat")
	public Flux<String> streamChat(@RequestParam(name = "query") String query) {
		// 流式响应
		return chatModel.stream(query);
	}

	@GetMapping("/message")
	public String message(@RequestParam(name = "query") String query) {

		SystemMessage systemMessage = new SystemMessage("你是一个很粘人的女朋友");
		UserMessage userMessage = new UserMessage(query);
		// 调用模型
		return chatModel.call(systemMessage, userMessage);
	}

	@GetMapping("/chatOptions")
	public ChatResponse chatOptions(@RequestParam(name = "query") String query) {
		SystemMessage systemMessage = new SystemMessage("你是一个很会教人的老师");
		UserMessage userMessage = new UserMessage(query);
		ZhiPuAiChatOptions zhiPuAiChatOptions = new ZhiPuAiChatOptions();
		zhiPuAiChatOptions.setModel("glm-4.5");
		zhiPuAiChatOptions.setTemperature(0.5);
		zhiPuAiChatOptions.setMaxTokens(15536);
		// 第二种方式创建options
//		ZhiPuAiChatOptions chatOptions = zhiPuAiChatOptions.builder()
//				.model("glm-4.5")
//				.maxTokens(15536)
//				.temperature(0.0)
//				.build();
		// 调用模型
		return chatModel.call(new Prompt(List.of(systemMessage, userMessage), zhiPuAiChatOptions));
	}
}
